package com.travel.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.travel.base.impl.BaseDaoImpl;
import com.travel.bean.Strategy;
import com.travel.dao.StrategyDao;

@Repository("strategyDaoImpl")
public class StrategyDaoImpl extends BaseDaoImpl<Strategy> implements StrategyDao {

	/**
	 * 按照分页查询所有的旅游攻略
	 */
	@Override
	public List<Strategy> queryByPage(Map map, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		Session session=this.hibernateTemplate.getSessionFactory().openSession();
		Criteria criteria=session.createCriteria(Strategy.class);
		if(map!=null){
			criteria.add(Restrictions.allEq(map));
		}
		
		if(pageNum!=null&&pageSize!=null){
			criteria.setFirstResult((pageNum-1)*pageSize);
			criteria.setMaxResults(pageSize);
		}
		List<Strategy> strategys=criteria.list();
		if(session!=null&&session.isOpen()){
			session.close();
		}
		return strategys;
	}

	/**
	 * 查询按条件分页的总记录数
	 */
	@Override
	public Integer queryByPageCount(Map map) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				Session session=this.hibernateTemplate.getSessionFactory().openSession();
				Criteria criteria=session.createCriteria(Strategy.class);
				if(map!=null){
					criteria.add(Restrictions.allEq(map));
				}
				criteria.setProjection(Projections.rowCount());
				Integer totalCount= ((Long)criteria.uniqueResult()).intValue();
				if(session!=null&&session.isOpen()){
					session.close();
				}
				return totalCount;
	}

}
